package dynamic.mybatisplus.core.injector.methods;



import com.baomidou.mybatisplus.core.enums.SqlMethod;

import dynamic.DynamicEntity;
import dynamic.ibatis.mapping.DMappedStatement;
import dynamic.ibatis.mapping.DSqlSource;
import dynamic.metadata.DTableInfo;
import dynamic.metadata.EntityClass;
import dynamic.mybatisplus.core.injector.DAbstractMethod;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

/**
 * 根据 ID 更新有值字段
 *
 * @author hubin
 * @since 2018-04-06
 */
public class UpdateById extends DAbstractMethod {

    public UpdateById() {
        this(SqlMethod.UPDATE_BY_ID.getMethod());
    }

    /**
     * @since 3.5.0
     * @param name 方法名
     */
    public UpdateById(String name) {
        super(name);
    }

    @Override
    public DMappedStatement injectMappedStatement(Class<?> mapperClass, EntityClass modelClass, DTableInfo tableInfo) {
        SqlMethod sqlMethod = SqlMethod.UPDATE_BY_ID;
        final String additional = optlockVersion(tableInfo) + tableInfo.getLogicDeleteSql(true, true);
        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(),
                sqlSet(tableInfo.isWithLogicDelete(), false, tableInfo, false, ENTITY, ENTITY_DOT),
                tableInfo.getKeyColumn(), ENTITY_DOT + tableInfo.getKeyProperty(), additional);
        DSqlSource sqlSource = super.createSqlSource(configuration, sql, DynamicEntity.class);
        return addUpdateMappedStatement(mapperClass, DynamicEntity.class, methodName, sqlSource);
    }
}
